import PrefabManager from "../code/xf/manager/PrefabManager";
import Plat from "../code/xf/Palt";
import SubLoad from "../code/xf/SubLoad";
import Log from "../code/xf/util/Log";
import AndroidTool from "../code/module/AndroidTool";
import ToolUtil from "../code/module/ToolUtil";
import SoundManager from "../code/manager/SoundManager";
import LData from "../code/manager/LData";


const {ccclass, property} = cc._decorator;

@ccclass
export class Load extends cc.Component 
{
   
    @property(cc.Sprite)
    bar: cc.Sprite = null;

    @property(cc.Label)
    version: cc.Label = null;

    @property(cc.Label)
    hint: cc.Label = null;

    
    onLoad () {
        var self=this
        let helper = {
            goGame : function(){
                self.adComplete=true
                self.goGame()
            }
        }
        window["load"]=helper;

        let sound = cc.sys.localStorage.getItem("sound");
        let playMusic = cc.sys.localStorage.getItem("playMusic");

        console.log("sound==>"+sound)
        console.log("playMusic==>"+playMusic)


        if(sound==null || sound==""){
            sound="true"
        }

        if(playMusic==null || playMusic==""){
            playMusic="true"
        }


        LData.yx ="true"==sound;
        LData.yy ="true"==playMusic;

        //this.version.string="v_"+Log.game_version+(Log.test==true?"_测试":"");

     //   cc.sys.localStorage.clear();

      //  cc.debug.setDisplayStats(false);

        this.flags[0]=0;

        this.scheduleOnce(this.startLoad,0.2);


    }

    public goGame(){
        if(!this.loadFinish){
            return
        }

        cc.director.loadScene("Main",()=>{
                
            Log.info("main场景加载成功")
        });
    }


    private flags:Array<number>=new Array();
    private loadFinish=false
    private adComplete=false

    public startLoad()
    {
        SubLoad.loadSub(()=>{

          
            PrefabManager.loadPrefabs((()=>{

                console.warn("预制体全部加载完成");

                cc.director.preloadScene("Main",()=>{
                    
                    Log.info("main场景预加载成功") 

                    this.loadFinish=true;

                    if(cc.sys.platform!=cc.sys.ANDROID){
                        this.goGame()
                        return
                    }

                    if(this.adComplete){
                        this.goGame()
                        return
                    }

                    AndroidTool.Instance.loadFinish()
                });


            }));
            
           
        })

         Plat.init();

     
     
        Log.info("---加载开始---")

        
    }

    private p=40;

    private frame_ok:boolean=false;

     update () 
     {
     
        this.p+=0.5;

        if(this.p>98)
        {
            this.p=98;
        }

        if(this.p<10)
        {
            this.p=10;
        }

        this.bar.node.width=500*this.p/100;

        if(ToolUtil.Instance.isZh()){
            this.hint.string="加载资源中... "+Math.floor(this.p)+"%";
        }


        if(this.p>90 && this.frame_ok==false)
        {

            

        
            this.frame_ok=true;
        }
       

    }
     
}

